/* @(#)GSBargainAddAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-4-20
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.upload.FormFile;

import com.hitrust.gs.bean.GSBargainBean;
import com.hitrust.gs.bean.GSBargainChargeBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSBargainProcessor;
import com.hitrust.gs.dao.GSBargainDAO;
import com.hitrust.gs.form.GSBargainAddForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.GenRandNum;
import com.hitrust.gs.util.exception.GSException;

/**
 * 增加合同信息和费用信息
 */
public class GSBargainAddAction extends GSBaseAction{

    
    public String execute(GSBaseForm aForm) throws Exception {
        GSBargainAddForm tForm = (GSBargainAddForm)aForm;
        GSBargainBean tGSBargainBean=new GSBargainBean();
        GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        try{
        BeanUtils.copyProperties(tGSBargainBean,tForm);
        //获取系统时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String buf = df.format(new java.util.Date(System.currentTimeMillis()));
        
        //判断是否可以增加合同信息
        GSBargainProcessor.permitAddBargainTask(tGSBargainBean);
        //文件上传并保存该文件
        FormFile tFile = tForm.getManufactureimage();
        //double fileSize = tFile.getFileSize();
        
       // byte[] buffer = tFile.getFileData();
        if(!(tFile.getFileSize() == 0)){
          //String tPath=tForm.getSession().getServletContext().getRealPath("upload");
            
          String tPath=GSSystemConfigurator.getParam("gs.upload.savepath");
          String tSaveName = tFile.getFileName();
          
          //文件系统时间
          SimpleDateFormat df1 = new SimpleDateFormat("MMddHHmmss");
          String buf1 = df1.format(new java.util.Date(System.currentTimeMillis()));
          
          String tSaveFileName=tSaveName.substring(0,tSaveName.indexOf("."));
          String tFileType=tSaveName.substring(tSaveName.indexOf("."));
          String tFileName=tSaveFileName+buf1+tFileType;
          
          GSBargainProcessor.saveFile(tFile,tPath,tFileName);
          tGSBargainBean.setGs_bgn_filename(tFileName);
        }
        //增加合同信息
        tGSBargainBean.setGs_bgn_id(GenRandNum.genDTRandNum());
        tGSBargainBean.setGs_bgn_recordid(tOperBean.getGs_oper_id());
        tGSBargainBean.setGs_bgn_recordtime(buf);
        
        //设置序号
        tForm.setGs_bgn_id(tGSBargainBean.getGs_bgn_id());
        
        if(tGSBargainBean.getGs_bgn_type().equals("2")){
          GSBargainDAO tGSBargainDAO=new GSBargainDAO();
          String tMainEctypeNum =tGSBargainDAO.queryBargainID(tGSBargainBean.getGs_bgn_mainserial()).getGs_main_ectype_num();    
          tGSBargainBean.setGs_main_ectype_num(tMainEctypeNum);
        }
        else{
            tGSBargainBean.setGs_main_ectype_num(buf);
        }
        
        if(tGSBargainBean.getGs_bgn_type().equals("1")){
            tGSBargainBean.setGs_bgn_mainserial(tGSBargainBean.getGs_bgn_serial());
        }
        
        GSBargainProcessor.addBargainTask(tGSBargainBean);
        
        //增加合同费用信息
        GSBargainChargeBean tGSBargainChargeBean=new GSBargainChargeBean();
        String[] tBgcType = tForm.getGs_bgc_type();
        String[] tBgcContent=tForm.getGs_bgc_content();
        for(int i=0;i<tBgcType.length;i++){
            if(!(tBgcType[i].equals("")&&tBgcContent[i].equals(""))){
            
            tGSBargainChargeBean.setGs_bgc_id(GenRandNum.genDTRandNum());
            tGSBargainChargeBean.setGs_bgc_bgn_id(tGSBargainBean.getGs_bgn_id());
            tGSBargainChargeBean.setGs_bgc_type(tBgcType[i]);
            tGSBargainChargeBean.setGs_bgc_content(tBgcContent[i]);
            
            GSBargainProcessor.addBargainChargeTask(tGSBargainChargeBean);
            }
          }
        }
        catch (GSException err) {
            if(err.getCode() != null ) { 
                HashMap tMap = new HashMap();
                tMap.put("err",err.getMessage());
                aForm.getRequest().setAttribute("_error",tMap);
                aForm.getRequest().setAttribute("exception_desc",err.getMessage());
                aForm.getRequest().setAttribute("exception_code",err.getCode());
                
                aForm.getRequest().setAttribute("gs_bgn_content",tForm.getGs_bgn_content());
                aForm.getRequest().setAttribute("gs_bgn_number",tForm.getGs_bgn_number());
                aForm.getRequest().setAttribute("gs_bgn_filenumber",tForm.getGs_bgn_filenumber());
                aForm.getRequest().setAttribute("gs_bgn_department",tForm.getGs_bgn_department());
                aForm.getRequest().setAttribute("gs_bgn_signuser",tForm.getGs_bgn_signuser());
                aForm.getRequest().setAttribute("gs_bgn_signdate",tForm.getGs_bgn_signdate());
                aForm.getRequest().setAttribute("gs_bgn_enddate",tForm.getGs_bgn_enddate());
                aForm.getRequest().setAttribute("gs_bgn_term",tForm.getGs_bgn_term());
                aForm.getRequest().setAttribute("gs_bgn_vol",tForm.getGs_bgn_vol());
                aForm.getRequest().setAttribute("gs_bgn_serial",tForm.getGs_bgn_serial());
                aForm.getRequest().setAttribute("gs_bgn_returneddate",tForm.getGs_bgn_returneddate());
                aForm.getRequest().setAttribute("gs_bgn_userid",tForm.getGs_bgn_userid());
                aForm.getRequest().setAttribute("type",tForm.getGs_bgn_usertype());
                aForm.getRequest().setAttribute("gs_bgn_remark",tForm.getGs_bgn_remark());
                aForm.getRequest().setAttribute("gs_bgn_week",tForm.getGs_bgn_week());
                
                String[] tBgcType = tForm.getGs_bgc_type();
                String[] tBgcContent=tForm.getGs_bgc_content();
                int num=0;
                for(int i=0;i<tBgcType.length;i++){
                    num++;
                    aForm.getRequest().setAttribute("gs_bgc_type"+num,tBgcType[i]);
                    aForm.getRequest().setAttribute("gs_bgc_content"+num,tBgcContent[i]);
                }
                
                aForm.getRequest().setAttribute("action", "A170101.do");
                return GSBaseConstants.FORWARD_ERROR;
            } 
        }
        aForm.getRequest().setAttribute("message", "新增合同信息成功!");
        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
